/**
 * Created by Administrator on 2017/6/27.
 */
define(['jquery', "vue"], function ($, Vue) {

    mui.init();

    // <editor-fold defaultstate="collapsed" desc="统计总额">
    function set_total_prcie() {
        var elems = document.querySelectorAll(".total_prcie_span");
        var total_price = 0;
        for (var i = 0, len = elems.length; i < len; i++) {
            total_price +=parseFloat(elems[i].innerHTML);
        }
        document.getElementById("total_price").innerHTML = total_price.toFixed(2);
    }
    // </editor-fold>

    var vue = new Vue({
        el: '#cart_page',
        data: {},
        methods: {
            all_check: function (id) {

                var item_total_price_span = document.getElementById("span_" + id);
                var item_total_price = parseFloat(item_total_price_span.innerHTML);
                var elems = document.getElementById(id).querySelectorAll("input[type='checkbox']");
                var target = event.currentTarget;
                var checked = target.checked;
                var s = [];
                for (var i = 0, len = elems.length; i < len; i++) {
                    if (!checked) {
                        if (elems[i].checked) {
                            s.push(elems[i].value);
                            var index = buy.indexOf(elems[i].value.toString());
                            if (index > -1) {
                                buy.splice(index, 1);
                            }
                        }
                        if (elems[i].disabled === false) {
                            elems[i].checked = false;
                        }
                    } else {
                        if (!elems[i].checked) {
                            s.push(elems[i].value);
                            var index = buy.indexOf(elems[i].value.toString());
                            if (index < 0) {
                                buy.push(elems[i].value.toString());
                            }
                        }
                        if (elems[i].disabled === false) {
                            elems[i].checked = true;
                        }
                    }
                }

                target.disabled = true;
                if (!checked) {
                    $.post(un_check_url, {cart_id: s.join(",")}, function (res) {
                            if (res.code === 1) {
                                item_total_price -= parseFloat(res.data.price);
                                item_total_price_span.innerHTML = item_total_price.toFixed(2);
                                set_total_prcie();
                            }
                            target.disabled = false;
                        }
                    )
                } else {
                    $.post(check_url, {cart_id: s.join(",")}, function (res) {
                            if (res.code === 1) {
                                item_total_price += parseFloat(res.data.price);
                                item_total_price_span.innerHTML = item_total_price.toFixed(2);
                                set_total_prcie();
                            }
                            target.disabled = false;
                        }
                    )
                }

            },
            check: function (cart_id, store_id, price) {
                var item_total_price_span = document.getElementById("span_" + store_id);
                var item_total_price = parseFloat(item_total_price_span.innerHTML);
                var checked = event.currentTarget.checked;
                var all_check = document.getElementById("ul_" + store_id).querySelector("input[type='checkbox'][name='all_check']");
                var elems = document.getElementById(store_id).querySelectorAll("input[type='checkbox']");
                var elems_check = document.getElementById(store_id).querySelectorAll("input[type='checkbox']:checked");

                if (checked) {
                    if (buy.indexOf(cart_id.toString()) === -1) {
                        buy.push(cart_id.toString());
                        var num = document.getElementById("li_" + cart_id).querySelector(".num").value;

                        var d_price = price * num;
                        item_total_price += parseFloat(d_price);
                        item_total_price_span.innerHTML = item_total_price.toFixed(2);
                        set_total_prcie();
                        $.post(check_url, {cart_id: cart_id}, function (res) {
                                if (res.code === 1) {
                                }
                            }
                        )
                    }
                    all_check.checked = true;
                } else {
                    var index = buy.indexOf(cart_id.toString());
                    if (index > -1) {
                        buy.splice(index, 1);
                        var num = document.getElementById("li_" + cart_id).querySelector(".num").value;
                        var d_price = price * num;
                        item_total_price -= parseFloat(d_price);
                        item_total_price_span.innerHTML = item_total_price.toFixed(2);
                        set_total_prcie();
                        $.post(un_check_url, {cart_id: cart_id}, function (res) {
                                if (res.code === 1) {
                                }
                            }
                        )
                    }
                    if (elems_check.length === 0) {
                        all_check.checked = false;
                    }
                }
            },
            edit: function (id, it_price, t_price) {
                var t = event.currentTarget.innerHTML;
                total_price = t_price;
                var text = document.getElementById(id).querySelectorAll(".text");
                var hidden = document.getElementById(id).querySelectorAll(".hidden");

                if (t === "编辑") {
                    for (var i = 0, len = text.length; i < len; i++) {
                        $(text[i]).hide();
                    }
                    for (var j = 0, len = hidden.length; j < len; j++) {
                        $(hidden[j]).show();
                    }
                    event.currentTarget.innerHTML = "完成";
                }
                else {
                    for (var i = 0, len = text.length; i < len; i++) {
                        var num_span = text[i].querySelector("span");
                        var n = hidden[i].querySelector(".num");
                        if(n){
                            num_span.innerHTML = $(n).val();
                        }

                        $(text[i]).show();
                    }
                    for (var j = 0, len = hidden.length; j < len; j++) {
                        $(hidden[j]).hide();
                    }
                    event.currentTarget.innerHTML = "编辑";
                }
            },
            detail: function (id) {
                window.location.href = detail_url + "/id/" + id + "/total_price/" + total_price;
            },
            down_tap: function (store_id, price, id, variant_id, cart_id) {

                var item_total_price_span = document.getElementById("span_" + store_id);
                var item_total_price = item_total_price_span.innerHTML;
                var target = $(event.currentTarget);
                var n = target.siblings('.num');

                var num = n.val();
                if (num === "1") {
                    return;
                }
                num--;

                n.val(num);
                if (num < 1) {
                    n.hide();
                    target.hide();
                }
                var li = document.getElementById("li_" + cart_id);
                var checkbox = li.querySelector("input[type='checkbox']");
                if (checkbox.checked) {
                    item_total_price = parseFloat(item_total_price) - parseFloat(price);
                    item_total_price_span.innerHTML = item_total_price.toFixed(2);
                }
                var data = {
                    "goods_id": id,
                    "type": "del",
                    "variant_id": variant_id,
                    "quantity": num
                };
                $.post(add_cart_url, data,
                    function (result) {
                        if (result.code === 1) {

                        }
                    });
                set_total_prcie();
            },
            up_tap: function (store_id, price, goods_id, variant_id, cart_id) {
                var item_total_price_span = document.getElementById("span_" + store_id);
                var item_total_price = item_total_price_span.innerHTML;
                var n = $(event.currentTarget).siblings('.num');
                var s = $(event.currentTarget).siblings('.down');
                var num = n.val();
                n.val(++num);

                n.show();
                s.show();
                var li = document.getElementById("li_" + cart_id);
                var checkbox = li.querySelector("input[type='checkbox']");
                if (checkbox.checked) {
                    item_total_price = parseFloat(item_total_price) + parseFloat(price);
                    item_total_price_span.innerHTML = item_total_price.toFixed(2);
                }

                var data = {
                    "goods_id": goods_id,
                    "variant_id": variant_id,
                    "type": "add",
                    "quantity": num
                };
                $.post(add_cart_url, data,
                    function (result) {
                        if (result.code === 1) {

                        }
                    });
                set_total_prcie();
            },
            del_btn: function (store_id, goods_id, price, cart_id) {
                mui.confirm('确定要删除该记录吗？', '', ['取消', '确定'], function (e) {
                    if (e.index === 1) {
                        $.post(delete_cart_item_url, {cart_id: cart_id}, function (res) {
                            if (res.code === 1) {
                                location.reload();
                                // var html='<div class="tip-icon" >'+
                                //     '<i class="iconfont icon-konggouwuche"></i>'+
                                //     '<p>空空如也</p>'+
                                //     '</div>';
                                // $('#cart_page').html(html);
                            } else {
                                mui.alert(res.msg)
                            }
                        })
                    }
                })
            },
            send_btn: function () {
                if (buy.length > 0) {
                    window.location.href = send_order_url + "/data/" + buy.join(",");
                }
                else {
                    mui.alert("您没购买任何商品");
                }
            }
        }
    });
});